package com.starinside.wp.repository;

import com.starinside.wp.foundation.vo.GrowupDayPlanVO;
import lombok.Data;
import org.springframework.stereotype.Repository;

import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Repository
public class GrowupNativeRepository extends BaseNativeSqlRepository{

    public static final Integer ORDER_PRICE_LOW = 8866;
    public static final Integer ORDER_PRICE_NORMAL = 12888;
    public static final Integer ORDER_PRICE_HIGH = 15888;

    public static final Integer FEED_TYPE_MOM = 1;
    public static final Integer FEED_TYPE_MIX = 2;
    public static final Integer FEED_TYPE_MANUAL = 3;


    public List getGrowupDayPlans(Integer month, Integer orderPrice, Integer leftday, Integer feedType){
//        String sql = "SELECT a.ID AS ID,a.timestamp AS timestamp,a.TestStore_ID AS TestStore_ID,DATE_FORMAT(a.From_Time, '%p %h:%i') AS From_Time,DATE_FORMAT(a.To_Time, '%p %h:%i')  AS To_Time," +
//                "a.Feed_Type AS Feed_Type,b.Type,a.Month,b.Title AS Title,b.Media AS media,b.Test_Type as testType FROM m_baby_defaulthandbook a " +
//                String.format("LEFT JOIN m_test_store b ON a.TestStore_ID = b.ID WHERE a.Month = %d AND a.Day = %d " , month, leftday) +
//                String.format("AND  %s " , (feedType==3?"(a.Feed_Type = 2 or a.Feed_Type=3)":"a.Feed_Type+"+feedType)) +
//                String.format("AND a.Order_Money = %d AND a.Is_Deleted = 0 AND b.Title IS NOT NULL ", orderPrice) +
//                "ORDER BY a.From_Time ASC";

        String sql = "SELECT a.ID AS ID,a.timestamp AS timestamp,a.TestStore_ID AS TestStore_ID,DATE_FORMAT(a.From_Time, '%H:%i') AS From_Time,DATE_FORMAT(a.To_Time, '%H:%i')  AS To_Time," +
                "a.Feed_Type AS Feed_Type,b.Type,a.Month,b.Title AS Title,b.Media AS media,b.Test_Type as testType FROM m_baby_defaulthandbook a " +
                "LEFT JOIN m_test_store b ON a.TestStore_ID = b.ID WHERE a.Month = ?1 AND a.Day = ?2 " +
                "AND  " + (feedType==3?"(a.Feed_Type = 2 or a.Feed_Type=3)":"a.Feed_Type="+feedType) +
                "AND a.Order_Money = ?3 AND a.Is_Deleted = 0 AND b.Title IS NOT NULL " +
                "ORDER BY a.From_Time ASC";

//        month, leftday,orderPrice
        List params = new ArrayList();
        params.add(month);
        params.add(leftday);
        params.add(orderPrice);

        List<GrowupDayPlanVO> items = sqlObjectList(sql, params,  GrowupDayPlanVO.class);


        @Data
        class GrowupDayPlanGroup{
            private String time;
            private List<GrowupDayPlanVO> datas;
        }

        List<GrowupDayPlanGroup> result = new ArrayList<>();

        GrowupDayPlanGroup curGroup = null;
        for (GrowupDayPlanVO planVO : items ) {

            String time = planVO.getFrom_Time();

            if (curGroup != null && ! time.equals(curGroup.time)){
                curGroup = null;
            }

            if (curGroup == null){
                curGroup = new GrowupDayPlanGroup();
                curGroup.setDatas(new ArrayList<>(5));
                curGroup.setTime(time);

                result.add(curGroup);
            }

            curGroup.getDatas().add(planVO);
        }

        result.forEach( k ->{
            k.setTime(k.getTime().replaceAll("AM","上午").replaceAll("PM", "下午"));
        });

        return result;
    }

}
